/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package MathLib.Geometry;

import java.io.BufferedOutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;

/**
 *
 * @author marko
 */
public class Polygon implements IFigure{
    public String Name="";
    protected int N;
    protected ArrayList<Point2DDouble> ListOfApexes;
    private double cx;
    private double cy;
    private double he;
    private double wi;
    
    public Polygon(){
        N = 0;
        ListOfApexes = new ArrayList<Point2DDouble>();
    }

    public Point2DDouble Get(int i){
        try{
            return new Point2DDouble(ListOfApexes.get(i));
        }catch(Exception e){
            return null;
        }
    }

    public int GetN(){
        return N;
    }
    public double GetX(int i){
        return ListOfApexes.get(i).x;
    }

    public double GetY(int i){
        return ListOfApexes.get(i).y;
    }

    public void Set(int i, Point2DDouble x){
        ListOfApexes.set(i, x);
    }

    public void SetX(int i, double x){
        Point2DDouble temp = ListOfApexes.get(i);
        temp.x = x;
        ListOfApexes.set(i, temp);
    }

    public void SetY(int i, double y){
        Point2DDouble temp = ListOfApexes.get(i);
        temp.y = y;
        ListOfApexes.set(i, temp);
    }

    public void Add(Point2DDouble x){
        N++;
        ListOfApexes.add(x);
    }

    public void Remove(int i){
        ListOfApexes.remove(i);
        N--;
    }
    public int[] X(){
        int[] x = new int[N];
        for (int i = 0; i < x.length; i++) {
            x[i] = (int)(cx*(ListOfApexes.get(i).x-he));
        }
        return x;
    }
    public int[] Y(){
        int[] y = new int[N];
        for (int i = 0; i < y.length; i++) {
            y[i] = (int)(cy*(wi - ListOfApexes.get(i).y));
        }
        return y;
    }

    public void setCoef(double x, double y,double h,double w){
        cx= x;
        cy =y;
        he = h;
        wi = w;
    }

    public boolean Include(Point2DDouble x) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void printToFile(BufferedOutputStream out) {
        PrintWriter o = new PrintWriter(out);
        o.println(ListOfApexes.size()+" 2 0 0");
        int n = ListOfApexes.size();
        for(int i=0;i<n;i++){
            Point2DDouble point = ListOfApexes.get(i);
            o.println((i+1)+" "+point.x+" "+point.y);
        }
        o.println(ListOfApexes.size()+" 0");
        for(int i=1;i<n;i++){
            o.println(i+" "+ i + " " + (i+1));
        }
        o.println(n+" "+ n + " " + 1);
        o.println("0");
        o.close();
    }

    public void printToFileQuad(BufferedOutputStream out) {
        PrintWriter o = new PrintWriter(out);
        o.println(ListOfApexes.size());
        int n = ListOfApexes.size();
        for(int i=0;i<n;i++){
            Point2DDouble point = ListOfApexes.get(i);
            o.println(point.x+" "+point.y+" 2");
        }
        o.println("0\n1");
        o.println(ListOfApexes.size()+" 0 0");
        for(int i=1;i<=n;i++){
            o.print(i + " ");
        }
        o.println();
        
        for(int i=1;i<=n;i++){
            o.print("2 ");
        }
        o.close();
    }
}
